package com.wiseMen.weixin.menu;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.sword.lang.HttpUtils;
import org.sword.wechat4j.token.TokenProxy;

public class WxCreateMenu {
	
	public static String readTxtFile(String filePath){
		StringBuffer result = new StringBuffer();;
        try {
                String encoding="UTF-8";
                File file=new File(filePath);
                if(file.isFile() && file.exists()){ //判断文件是否存在
                    InputStreamReader read = new InputStreamReader(
                    new FileInputStream(file),encoding);//考虑到编码格式
                    BufferedReader bufferedReader = new BufferedReader(read);
                    String lineTxt = null;
                    while((lineTxt = bufferedReader.readLine()) != null){
                        result.append(lineTxt.trim());
                    }
                    read.close();
        }else{
            System.out.println("找不到指定的文件");
        }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return result.toString();
    }
	
	public static void main(String[] args) {
		String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
		String path = WxCreateMenu.class.getClassLoader().getResource("//").getPath()+"com/wiseMen/weixin/menu/WxMenu.txt";
		String menuJson = readTxtFile(path);
		String accessToken = TokenProxy.accessToken();
		url = url+accessToken;
		String resultJson = HttpUtils.post(url,menuJson);
		System.out.println(resultJson);
	}
}
